package com.gxuwz.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.gxuwz.model.Academy;
import com.gxuwz.util.DBUtil;

/**
 * 院校DAO
 * 
 * @author h
 * 
 */
public class AcademyDao {
	private static Connection conn = null;
	private static Statement stmt = null;
	private static PreparedStatement ptmt = null;
	private static ResultSet rs = null;

	public static void save(List<Academy> academys){
		try{
		conn = DBUtil.getConnection();
		stmt = conn.createStatement();
		for (Academy academy : academys) {
				System.out.println(academy.getAcademyName()+"------"+academy.getAcademyURL());
				stmt.execute("insert into academy(academyName,academyURL) values('"
					+ academy.getAcademyName() + "','" + academy.getAcademyURL()
					+ "')");
		}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try {
				stmt.close();
				conn.close();				
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	public static boolean queryByName(String academyName){
		boolean isResult = false;
		try{
		conn = DBUtil.getConnection();
		ptmt = conn.prepareStatement("select * from academy where academyName = ?");
		ptmt.setString(1, academyName);
		rs = ptmt.executeQuery();
		while(rs.next()){
			isResult = true;
		}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try {
				ptmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return isResult;
	}
	
	public static List<Academy> queryAll(){
		List<Academy> academies = new ArrayList<>();
		Academy academy = null;
		try{
		conn = DBUtil.getConnection();
		stmt = conn.createStatement();
		rs = stmt.executeQuery("select * from academy");
		while(rs.next()){
			academy = new Academy();
			academy.setId(rs.getInt("id"));
			academy.setAcademyName(rs.getString("academyName"));
			academy.setAcademyURL(rs.getString("academyURL"));
			academies.add(academy);
		}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try {
				rs.close();
				stmt.close();
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return academies;
	}
	
}
